package com.matrix.codeGeneration.ext;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.DataFormatter;

import com.matrix.codeGeneration.model.CodeFile;
import com.matrix.codeGeneration.model.CodeModel;
import com.matrix.codeGeneration.model.CommonData;
import com.matrix.codeGeneration.model.Constant;
import com.matrix.codeGeneration.model.OutDataSource;
import com.matrix.codeGeneration.model.TableClassModel;
import com.matrix.codeGeneration.plugin.DateUtils;

/**
 * 平安意健险代码文件模型
 */
public class AhcsModels implements CodeModel {

	/**
	 * 模型名称用来标识这个模型
	 */
	String modelName="";

	/**
	 * 包名
	 */
	private String packageName="";

	/**
	 * 名称后缀
	 */
	private String suffixName="";

	/**
	 * 名称前缀
	 */
	private String prefixName="";

	/**
	 * 文件扩展名称
	 */
	private String fileExtName="";

	/**
	 * 模板名称
	 */
	private String templateName="";

	/**
	 * 导入类列表
	 */
	private Map<String, String> importList = new HashMap<>();

	/**
	 * 类的变量名称
	 */
	private String classVariableName="";

	   

	@Override
	public List<CodeFile> buildCodeFile(OutDataSource dataSource) {
		List<CodeFile> codeFileList = new ArrayList<>();
		for (TableClassModel tableClassModel : dataSource.getTableClassModels()) {
			Map<String, Object> fileData = new HashMap<>();
			System.out.println(tableClassModel.getClassName());
			System.out.println(this.modelName);
			System.out.println("--------------");

			if (Constant.JAVA_BEAN.equals(this.modelName)) {

				this.getImportList().put("Extend", "com.zkingsoft.anotations.Extend");

			} else if (Constant.MYBATIS_DAO.equals(this.modelName)) {
				this.setImportList(new HashMap<String, String>());
				this.getImportList().put("bean",
						getModelQualifiedClassName(Constant.JAVA_BEAN, tableClassModel, dataSource));

				this.getImportList().put("List", "java.util.List");
				this.getImportList().put("Param", "org.apache.ibatis.annotations.Param");
				this.getImportList().put("PaginationVO", "com.zkingsoft.pojo.PaginationVO");
				this.getImportList().put("Map", "java.util.Map");

			} else if (Constant.MYBATIS_DAO_IMPL.equals(this.modelName)) {

			} else if (Constant.SERVICE.equals(this.modelName)) {

				this.setImportList(new HashMap<String, String>());
				this.getImportList().put("bean",
						getModelQualifiedClassName(Constant.JAVA_BEAN, tableClassModel, dataSource));
				this.getImportList().put("List", "java.util.List");
				this.getImportList().put("BaseServices", "com.zkingsoft.constraint.BaseServices");
				this.getImportList().put("PaginationVO", "com.zkingsoft.pojo.PaginationVO");

			} else if (Constant.SERVICE_IMPL.equals(this.modelName)) {
				this.setImportList(new HashMap<String, String>());
				this.getImportList().put("bean",
						getModelQualifiedClassName(Constant.JAVA_BEAN, tableClassModel, dataSource));

				this.getImportList().put("services",
						getModelQualifiedClassName(Constant.SERVICE, tableClassModel, dataSource));
				this.getImportList().put("dao",
						getModelQualifiedClassName(Constant.MYBATIS_DAO, tableClassModel, dataSource));
				this.getImportList().put("springframework.Service", "org.springframework.stereotype.Service");
				this.getImportList().put("Autowired", "org.springframework.beans.factory.annotation.Autowired");
				this.getImportList().put("List", "java.util.List");
				this.getImportList().put("Map", "java.util.Map");

			} else if (Constant.ACTION.equals(this.modelName)) {

				this.getImportList().put("Controller", "org.springframework.stereotype.Controller");
				this.getImportList().put("Resource", "javax.annotation.Resource");
				this.getImportList().put("AjaxResult", "com.zkingsoft.pojo.AjaxResult");
				this.getImportList().put("PaginationVO", "com.zkingsoft.pojo.PaginationVO");
				this.getImportList().put("WebUtil", "com.zkingsoft.util.WebUtil");
				this.getImportList().put("SaveRequestToken", "com.zkingsoft.anotations.SaveRequestToken");
				this.getImportList().put("RemoveRequestToken", "com.zkingsoft.anotations.RemoveRequestToken");
				this.getImportList().put("BaseController", "com.zkingsoft.constraint.BaseController");
				this.getImportList().put("RequestMapping", "org.springframework.web.bind.annotation.RequestMapping");
				this.getImportList().put("ResponseBody", "org.springframework.web.bind.annotation.ResponseBody");
			}
			fileData.put("importList", this.getImportList());
			CodeFile codeFile = new CodeFile();
			// 计算文件的名称
			codeFile.setFileName(getFileName(tableClassModel, this));
			codeFile.setSavePath(File.separator + getPackageName().replace(".", File.separator) + File.separator);
			fileData.put("codeModel", this);
			fileData.put("dataSource", dataSource);
			fileData.put("tableClassModel", tableClassModel);
			fileData.put("commonData", new CommonData());
			fileData.put("time", DateUtils.dateFormatStr(new Date(), "yyyy-MM-dd HH:mm"));

			fileData.put("ClassName", getModelClassName(this.getModelName(), tableClassModel, dataSource));
			codeFile.setData(fileData);
			codeFile.setTemplateName(this.templateName);
			codeFileList.add(codeFile);
		}
		return codeFileList;

	}

	/**
	 * 计算文件名称
	 * 
	 * @param tableClassModel
	 * @return
	 */
	public String getFileName(TableClassModel tableClassModel, AhcsModels ahcsModels) {
		StringBuffer name = new StringBuffer();
		String prefix = ahcsModels.getPrefixName();
		String suffix = ahcsModels.getSuffixName();
		String extensionName = ahcsModels.getFileExtName();
		if (prefix != null) {
			name.append(prefix);
		}
		name.append(tableClassModel.getClassName());
		if (suffix != null) {
			name.append(suffix);
		}
		name.append(extensionName);
		return name.toString();
	}

	/**
	 * 获取一个模型的变量名
	 * 
	 * @param modelName
	 * @param dataSource
	 * @return
	 */
	public String getModelVariablesName(String modelName, TableClassModel tableClassModel, OutDataSource dataSource) {
		return dataSource.getNameConvert()
				.classNameToTableName(getModelClassName(modelName, tableClassModel, dataSource));
	}

	/**
	 * 获取一个模型的全类名
	 * 
	 * @param modelName
	 * @param dataSource
	 * @return
	 */
	public String getModelQualifiedClassName(String modelName, TableClassModel tableClassModel,
			OutDataSource dataSource) {
		AhcsModels ahcsMOdels = ((AhcsModels) dataSource.getCodeModels().get(modelName));
		String className = ahcsMOdels.getPackageName() + "." + ahcsMOdels.getPrefixName()
				+ tableClassModel.getClassName() + ahcsMOdels.getSuffixName();
		return className;
	}

	/**
	 * 获取一个模型的类名
	 * 
	 * @param modelName
	 * @param dataSource
	 * @return
	 */
	public String getModelClassName(String modelName, TableClassModel tableClassModel, OutDataSource dataSource) {
		AhcsModels ahcsMOdels = ((AhcsModels) dataSource.getCodeModels().get(modelName));
		String className = ahcsMOdels.getPrefixName() + tableClassModel.getClassName() + ahcsMOdels.getSuffixName();
		return className;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getSuffixName() {
		return suffixName;
	}

	public void setSuffixName(String suffixName) {
		this.suffixName = suffixName;
	}

	public String getPrefixName() {
		return prefixName;
	}

	public void setPrefixName(String prefixName) {
		this.prefixName = prefixName;
	}

	public String getFileExtName() {
		return fileExtName;
	}

	public void setFileExtName(String fileExtName) {
		this.fileExtName = fileExtName;
	}

	public String getTemplateName() {
		return templateName;
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

	public Map<String, String> getImportList() {
		return importList;
	}

	public void setImportList(Map<String, String> importList) {
		this.importList = importList;
	}

	public String getClassVariableName() {
		return classVariableName;
	}

	public void setClassVariableName(String classVariableName) {
		this.classVariableName = classVariableName;
	}
 
 

}
